<?php

/*
 * Phlat - FlatFiles PHP Interface
 * Phlat is a simple interface to work easily with flatfiles
 * Programmed by Federico Ramirez (aka fedekun) 
 * Last Updated: 27 june 2009
 * This class is licenced under the GPL licence
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */ 
 




/*
 * This is an example of FDB usage
 */
 
 
 
 include('phlat.class.php');
 
 
 
 /*
  *  Creates a new table - Note: All tables must have an 'id' field. If you dont put it here
  *  it will be added automatically.
  */
$db = new phlat('usersDb', array('user', 'pass', 'date'));
$db->clear(); // Erase all possible entries already there



/*
 * Just use an array to add data, format is field=>value
 * If you use a field not specified when creating the db, it will be ignored.
 * If you leave one field empty, value will be null
 */
$db->add(array('user'=>'test1', 'pass'=>'pass1'));
$db->add(array('user'=>'test2', 'pass'=>'pass2'));
$db->add(array('user'=>'test3', 'pass'=>'pass3'));
$db->add(array('user'=>'test4', 'pass'=>'pass4'));



/*
 * Shows all database data 
 */
echo '<b>Showing ALL db data.</b><br />';
print_r(
	$db->get()
);
echo '<br /><br /><br />';



/*
 * Edit data
 * You cannot edit the id field
 */
$db->editAt('user', 'test1', array('user'=>'edited test1', 'pass'=>'321654', 'date'=>'Now'));



/*
 * Shows all database data again
 */
echo 'Modified test1 - <b>Showing ALL db data.</b><br />';
print_r(
	$db->get()
);
echo '<br /><br /><br />';



/*
 * Select first 2 users
 */
 echo 'Showing first 2 users.<br />';
print_r(
	$db->selectRange(0, 1)
);
echo '<br /><br /><br />';



/*
 * Select last 2 users
 */
  echo 'Showing last 2 users.<br />';
print_r(
	$db->selectRange($db->getSize()-2)
);
echo '<br /><br /><br />';



/*
 * Select users with ids between 2 and 3
 */
  echo 'Showing users with ids between 2 and 3.<br />';
print_r(
	$db->selectRangeAt(2, 3)
);
echo '<br /><br /><br />';



/*
 * Select user with id 3
 */
  echo 'Showing user with id 3.<br />';
print_r(
	$db->selectAt('id', 3)
);
echo '<br /><br /><br />';



/*
 * Select user with user=test2 AND pass=pass2
 * Only values valid for parameter 2 are AND and OR
 */
  echo 'Showing user with user=test2 AND pass=pass2.<br />';
print_r(
	$db->selectComplex(array('user'=>'test2', 'pass'=>'pass2'), 'AND')
);
echo '<br /><br /><br />';



/*
 * Delete latest 2 users
 */
$db->deleteRange($db->getSize()-2);
echo 'Delete latest 2 users - <b>Showing ALL db data.</b><br />';
print_r(
	$db->get()
);
echo '<br /><br /><br />';



/*
 * Delete users with ids from 1 to 4
 * $db->deleteRangeAt(1, 4);
 */



/*
 * Delete user with user=test2
 */
$db->deleteAt('user', 'test2');
echo 'Delete user with user=test2 - <b>Showing ALL db data.</b><br />';
print_r(
	$db->get()
);
echo '<br /><br /><br />';



/*
 * DB Data
 */
 echo 'Table Fields: ';
 print_r($db->getTableFields());
 
 echo '<br />Table Name: ' . $db->getTableName() . '<br />
 Table Entries: ' . $db->getSize();
?>